/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.scene.Spatial;
import game.TankControl;

/**
 *
 * @author apple
 */
public class PlayerController implements AnalogListener{

    private final float MOUSE_TO_DEGREES = 90f;

    private InputManager inputManager;
    private Spatial tank;
    
    public PlayerController(InputManager inputManager, Spatial tank) {
        this.tank = tank;
        this.inputManager = inputManager;
        inputManager.addMapping("move forward", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("move backward", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("move left", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("move right", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("turn horizontally", new MouseAxisTrigger(
                    MouseInput.AXIS_X,true));

        
    }
    
    
    
    public void init() {
        String[] mappingNames = {"move forward", "move backward", "move left",
            "move right", "turn horizontally", "turn vertically"};
        inputManager.addListener(this, mappingNames);
    }
    
    
    public void onAnalog(String name, float value, float tpf) {
        if (tank != null) {
                TankControl tankControl = tank.getControl(TankControl.class);
                if (tankControl != null) {
                    if (name.equals("move forward")) {
                        tankControl.goForward(tpf);
                    }
                    else if (name.equals("move backward")) {
                        tankControl.goBackward(tpf);
                    }
                    else if (name.equals("move left")) {
                        tankControl.goLeft(tpf);
                    }
                    else if (name.equals("move right")) {
                        tankControl.goRight(tpf);
                    }
                    else if (name.equals("turn horizontally")) {
                        tankControl.turn(value*tpf*MOUSE_TO_DEGREES);
                    }
                }
            }
    }
    
}
